package com.yunkss.eftool.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

/**
 * @Author csx
 * @Date 2024/8/28 22:31
 * @Desc WebFluxConfig
 **/
@Configuration
public class WebFluxConfig {

    @Bean
    public RouterFunction<ServerResponse> globalErrorHandlerRoute() {
        return RouterFunctions.route(
                RequestPredicates.GET("/eftool/401"),  // 假设有一个API路径触发401错误
                request -> Mono.empty() // 这里抛出异常来模拟401
                        .flatMap(
                                // 自定义异常处理器
                                ex -> ServerResponse.status(HttpStatus.UNAUTHORIZED).body(ex.toString(), String.class)
                        )
        );
    }

}
